local Loveframes = require"library/loveframes"

local room = class("RoomScene",require"game/scene"()) {
    ui = nil;
    select_room_button = nil,
    select_room = "",
    room_items = {},
    window_width = 350;
    window_height = 600;
}

function room:_init()
    self.ui = Loveframes();
    self.name = "Room";
    self:_initUI()
    Game.Network:addSigbalConnect("JoinRoomSucceed",self,self._JoinRoomSucceed)
    Game.Network:addSigbalConnect("GetRoomList",self,self._GetRoomList)
end

function room:_enter()
    Game.Network.Sock:send("GetRoomList")
    love.window.setMode(self.window_width,self.window_height);
end

function room:_initUI()
    for _, skin in pairs(self.ui.skins) do
        skin.controls.smallfont = Game.FONT;
        skin.controls.imagebuttonfont = Game.FONT;
    end

    local panel_offset = 5;
    local window_offset = 5;
    local ox = window_offset;
    local oy = window_offset;
    local width = self.window_width - (window_offset * 2)
    local height = self.window_height - (window_offset * 2)
    
    local room_select_panel_x = ox;
    local room_select_panel_y = oy;
    local room_select_panel_w = width
    local room_select_panel_h = height * 0.85;

    local button_panel_x = room_select_panel_x
    local button_panel_y = room_select_panel_y + room_select_panel_h + panel_offset
    local button_panel_w = width;
    local button_panel_h = height - room_select_panel_h - panel_offset;
    local button_number = 2
    local button_w = button_panel_w / button_number
    local button_h = button_panel_h

    self.room_select_panel = self.ui.Create("list")
        :SetPos(room_select_panel_x,room_select_panel_y)
        :SetSize(room_select_panel_w,room_select_panel_h)

    self.button_panel = self.ui.Create("grid")
        :SetSize(button_panel_w,button_panel_h)
        :SetPos(button_panel_x,button_panel_y)
        :SetRows(1)
        :SetColumns(2)
        :SetCellPadding(0)
        :SetCellWidth(button_w)
        :SetCellHeight(button_h)
        :SetItemAutoSize(true)
        ;

    self.create_room_button = self.ui.Create("button"):SetText("创建房间",self.button_panel);
    self.enter_room_button = self.ui.Create("button"):SetText("加入房间",self.button_panel);
    self.create_room_button.OnClick = function ()
        Game:switchScene("CreateRoom")
    end
    self.enter_room_button.OnClick = function ()
        Game.Network:requestJoinRoom(self.select_room)
    end

    self.button_panel:AddItem(self.create_room_button,1,1)
    self.button_panel:AddItem(self.enter_room_button,1,2)
end

function room:ClearRoomList()
    for i,item in pairs(self.room_items) do
        self.room_select_panel:RemoveItem(item)
        item:Remove();
    end
    self.room_items = {}
end

function room:_JoinRoomSucceed(pack)
    Game:switchScene("Game");

    local homeowners = pack.homeowners
    local room = pack.room
    local game_scene = Game.scene;
    game_scene.player1.label = homeowners;
    game_scene.player2.label = Game.Network.name;
    Game.Network.player_port = Game.PLAYER_PORT_B;
    Game.Network.room = room
end

function room:_GetRoomList(pack)
    self:ClearRoomList()
    for i,item in ipairs(pack.list) do
        local room_item = self.ui.Create("button")
            :SetText(item)
            :Center()
        room_item.OnClick = function ()
            if self.select_room_button ~= nil then
                self.select_room_button.checked = false;
            end
            self.select_room_button = room_item
            self.select_room = item;
            room_item.checked = true;
        end
        self.room_items[item] = room_item;
        self.room_select_panel:AddItem(room_item)
    end
end

function room:_draw()self.ui:draw() end
function room:_update(dt) self.ui.update(dt) end
function room:_mousepressed(x, y, button) self.ui.mousepressed(x, y, button) end
function room:_mousereleased(x, y, button) self.ui.mousereleased(x, y, button) end
function room:_wheelmoved(x, y) self.ui.wheelmoved(x, y) end
function room:_keypressed(k, scancode, isrepeat) self.ui.keypressed(k, isrepeat) end
function room:_keyreleased(key) self.ui.keyreleased(key) end
function room:_textinput(text) self.ui.textinput(text) end

return room;